<!DOCTYPE HTML>
<html lang="zh-CN">


<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="基于STM32的智能天气预报系统, ZhengN_Blog">
    <meta name="description" content="1、作品介绍首先，看一下作品的演示视频：
天气预报系统
温馨提示：因为是进行人机对话演示，所以应打开音量观看哈。

这是本人的毕业设计，一个智能的天气预报系统。显示屏上显示各种天气指标及实时显示时间日期等。可以使用触摸屏输入城市名称搜索天气">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>基于STM32的智能天气预报系统 | ZhengN_Blog</title>
    <link rel="icon" type="image/jpeg" href="/favicon.jpg">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <style type="text/css">
        
    </style>

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">ZhengN_Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>友情链接</span>
        </a>
    </li>
    
    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">ZhengN_Blog</div>
        <div class="logo-desc">
            
            printf(&#34;每天进步一点点~&#34;);
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                友情链接
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/zhengnianli/stm32_weather" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/zhengnianli/stm32_weather" class="github-corner tooltipped hide-on-med-and-down" target="_blank" data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewbox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/>
    </svg>
</a>
        
    </nav>

</header>





<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/8.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        基于STM32的智能天气预报系统
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 20px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/嵌入式/" target="_blank">
                                <span class="chip bg-color">嵌入式</span>
                            </a>
                        
                            <a href="/tags/STM32/" target="_blank">
                                <span class="chip bg-color">STM32</span>
                            </a>
                        
                            <a href="/tags/作品/" target="_blank">
                                <span class="chip bg-color">作品</span>
                            </a>
                        
                            <a href="/tags/天气预报/" target="_blank">
                                <span class="chip bg-color">天气预报</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fa fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/嵌入式/" class="post-category" target="_blank">
                                嵌入式
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                <div class="post-date info-break-policy">
                    <i class="fa fa-calendar-minus-o fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-06-20
                </div>

                
                    
                    <div class="info-break-policy">
                        <i class="fa fa-file-word-o fa-fw"></i>文章字数:&nbsp;&nbsp;
                        5.5k
                    </div>
                    

                    
                    <div class="info-break-policy">
                        <i class="fa fa-clock-o fa-fw"></i>阅读时长:&nbsp;&nbsp;
                        21 分
                    </div>
                    
                
				
				
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="fa fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="1、作品介绍"><a href="#1、作品介绍" class="headerlink" title="1、作品介绍"></a>1、作品介绍</h2><p>首先，看一下作品的演示视频：</p>
<div align="center"><iframe frameborder="0" src="https://v.qq.com/txp/iframe/player.html?vid=t0779besn7h" allowfullscreen width="600" height="400" align="center">天气预报系统</iframe></div>
温馨提示：因为是进行人机对话演示，所以应打开音量观看哈。

<p>这是本人的毕业设计，一个智能的天气预报系统。显示屏上显示各种天气指标及实时显示时间日期等。可以使用触摸屏输入城市名称搜索天气，也可以使用语音搜索天气。</p>
<h3 id="1-1-系统功能"><a href="#1-1-系统功能" class="headerlink" title="1.1 系统功能"></a>1.1 系统功能</h3><p>作品包含的的功能有：</p>
<p>（1）实时天气显示，温湿度显示，日历显示；</p>
<p>（2）收音机功能；</p>
<p>（3）人机对话功能。</p>
<p>系统框图如下：</p>
<p><a href="https://imgchr.com/i/ZkGtI0" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkGtI0.md.png" alt="系统框图"></a></p>
<h3 id="1-2-系统GUI界面"><a href="#1-2-系统GUI界面" class="headerlink" title="1.2 系统GUI界面"></a>1.2 系统GUI界面</h3><p><strong>（1）主界面</strong></p>
<p><a href="https://imgchr.com/i/ZkG6d1" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkG6d1.md.png" alt="主界面"></a></p>
<p>你没有看错，就是99℃，就是星期八。但这不是系统出错，而是本人故意设置的初始值，每当开机收到天气数据之后就可以看出有明显的变化。</p>
<p><strong>（2）菜单界面</strong></p>
<p><a href="https://imgchr.com/i/ZkGfzD" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkGfzD.md.png" alt="菜单界面"></a></p>
<p><strong>（3）wifi设置界面</strong></p>
<p><a href="https://imgchr.com/i/ZkGILd" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkGILd.md.png" alt="wifi设置界面"></a></p>
<p>点击文本框会进入键盘界面，输入WiFi信息之后返回，再点击<code>Add</code>按钮即可发送WiFi名称与密码给控制器，控制器控制WiFi模块连接WiFi热点。</p>
<p><strong>（4）收音机界面</strong></p>
<p><a href="https://imgchr.com/i/ZkGTeA" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkGTeA.md.png" alt="收音机界面"></a></p>
<p>通过点击下方频率点跳到相应频率，再通过左右按钮调节频率至所需频率。</p>
<h2 id="2、作品实现"><a href="#2、作品实现" class="headerlink" title="2、作品实现"></a>2、作品实现</h2><h3 id="2-1-天气数据获取及解析"><a href="#2-1-天气数据获取及解析" class="headerlink" title="2.1 天气数据获取及解析"></a>2.1 天气数据获取及解析</h3><h3 id="2-1-1-天气数据从哪来？"><a href="#2-1-1-天气数据从哪来？" class="headerlink" title="2.1.1 天气数据从哪来？"></a>2.1.1 天气数据从哪来？</h3><p>天气数据可以从一些专门做天气预报的网站获取，如心知天气、和风天气等。本人选择的是心知天气</p>
<blockquote>
<p><a href="https://www.seniverse.com/" target="_blank" rel="noopener">https://www.seniverse.com/</a></p>
</blockquote>
<p>网站首页如下：</p>
<p><a href="https://imgchr.com/i/ZkJQFx" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkJQFx.md.png" alt></a></p>
<p>我们是通过其<code>API密钥</code>才能获取得到其天气数据，而只有注册的用户才拥有API密钥，所以必须得注册，可以点击右上角进行注册。</p>
<h3 id="2-1-2-天气数据是什么格式？"><a href="#2-1-2-天气数据是什么格式？" class="headerlink" title="2.1.2 天气数据是什么格式？"></a>2.1.2 天气数据是什么格式？</h3><p>登录<code>心知天气</code>网站之后，点击菜单导航中的<code>数据-&gt;常规数据</code>即可查看<code>API文档</code>。在API文档页面的左侧可看到一些可查看的条目，如：</p>
<p><img src="https://s2.ax1x.com/2019/06/24/ZkJL7R.png" alt></p>
<p>可点击<code>天气实况</code>查看其相关说明，可以看到其天气数据格式如下图所示：</p>
<p><a href="https://imgchr.com/i/ZkYS1O" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkYS1O.md.png" alt></a></p>
<p>这就是JSON格式的数据，不了解JSON的朋友可查看上一篇笔记：<a href="https://zhengnianli.github.io/2019/06/15/json-de-jian-dan-ren-shi/" target="_blank" rel="noopener">JSON的简单认识</a></p>
<h3 id="2-1-3-如何解析得到有用的数据？"><a href="#2-1-3-如何解析得到有用的数据？" class="headerlink" title="2.1.3 如何解析得到有用的数据？"></a>2.1.3 如何解析得到有用的数据？</h3><p>从上图中的JSON格式天气数据包中我们可以看出：我们需要用到的数据就是<code>冒号后面的字符串数据</code>，这些数据是我们需要获取并显示到屏幕上的数据。</p>
<p>那么，我们该怎么从这一堆JSON格式数据中解析出冒号后面的字符串呢？并且，这个系统是基于单片机的天气预报系统。而单片机使用C语言进行编程开发的，所以我们得使用C语言对这些JSON天气数据包进行解析。</p>
<p>其实，有一个专门解析JSON数据包的第三方C语言库。我们可以使用这个库进行解析，这个CJSON库的下载链接为：</p>
<blockquote>
<p>链接：<a href="https://pan.baidu.com/s/1DQynsdlNyIvsVXmf4W5b8Q" target="_blank" rel="noopener">https://pan.baidu.com/s/1DQynsdlNyIvsVXmf4W5b8Q</a><br>提取码：ww4z </p>
</blockquote>
<p>只要把<code>cJSON.c</code>与<code>cJSON.ｈ</code>放到工程主程序所在目录，然后在主程序中包含头文件<code>JSON.ｈ</code>即可引入该库。如：</p>
<p><img src="https://s2.ax1x.com/2019/06/24/ZkBVlq.png" alt></p>
<p>下面给出一个实例：</p>
<p><strong>测试代码：</strong></p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">/*----------------------------------------------------------------------------------------

    Program Explain：解析JSON天气数据包now.json（天气实况）
    Create Date：2017.12.6 by lzn
----------------------------------------------------------------------------------------*/</span>

<span class="token comment" spellcheck="true">//1、数据来源：心知天气（api.seniverse.com）</span>
<span class="token comment" spellcheck="true">//2、获取方法：GET https://api.seniverse.com/v3/weather/now.json?key=2owqvhhd2dd9o9f9&amp;location=beijing&amp;language=zh-Hans&amp;unit=c</span>
<span class="token comment" spellcheck="true">//3、返回的数据范例见文件test.txt</span>

<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;string.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">"cJSON.h"</span></span>

<span class="token comment" spellcheck="true">//函数声明</span>
<span class="token keyword">int</span> <span class="token function">cJSON_WeatherParse</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>JSON<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/*********************************************************************************
* Function Name    ： main主函数
* Parameter           ： NULL
* Return Value     ： 0 
* Function Explain ： 
* Create Date      ： 2017.12.6 by lzn
**********************************************************************************/</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>argv<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    FILE <span class="token operator">*</span>fp<span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span>data<span class="token punctuation">;</span>
    <span class="token keyword">int</span> len<span class="token punctuation">;</span>
    <span class="token keyword">int</span> i<span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>fp <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string">"now.txt"</span><span class="token punctuation">,</span><span class="token string">"rb"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Open error!\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> 
    <span class="token function">fseek</span><span class="token punctuation">(</span>fp<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> SEEK_END<span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">//文件指针指向文件末尾</span>
    len <span class="token operator">=</span> <span class="token function">ftell</span><span class="token punctuation">(</span>fp<span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">//求文件长度 </span>
    <span class="token function">fseek</span><span class="token punctuation">(</span>fp<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> SEEK_SET<span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">//文件指针指向文件开头</span>
    data <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token function">malloc</span><span class="token punctuation">(</span>len<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">fread</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> len<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> fp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">fclose</span><span class="token punctuation">(</span>fp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//printf("read file %s complete, len=%d.\n","now.txt",len);</span>
    <span class="token function">cJSON_WeatherParse</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment" spellcheck="true">//解析天气数据</span>
    <span class="token function">free</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">system</span><span class="token punctuation">(</span><span class="token string">"pause"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/*********************************************************************************
* Function Name    ： cJSON_WeatherParse,解析天气数据
* Parameter           ： JSON：天气数据包  results：保存解析后得到的有用的数据
* Return Value     ： 0：成功 其他:错误
* Function Explain ： 
* Create Date      ： 2017.12.6 by lzn
**********************************************************************************/</span>
<span class="token keyword">int</span> <span class="token function">cJSON_WeatherParse</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>JSON<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    cJSON <span class="token operator">*</span>json<span class="token punctuation">,</span><span class="token operator">*</span>arrayItem<span class="token punctuation">,</span><span class="token operator">*</span>object<span class="token punctuation">,</span><span class="token operator">*</span>subobject<span class="token punctuation">,</span><span class="token operator">*</span>item<span class="token punctuation">;</span>

    json <span class="token operator">=</span> <span class="token function">cJSON_Parse</span><span class="token punctuation">(</span>JSON<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//解析JSON数据包</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>json <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>          <span class="token comment" spellcheck="true">//检测JSON数据包是否存在语法上的错误，返回NULL表示数据包无效</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Error before: [%s]\n"</span><span class="token punctuation">,</span><span class="token function">cJSON_GetErrorPtr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//打印数据包语法错误的位置</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>arrayItem <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>json<span class="token punctuation">,</span><span class="token string">"results"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//匹配字符串"results",获取数组内容</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">int</span> size <span class="token operator">=</span> <span class="token function">cJSON_GetArraySize</span><span class="token punctuation">(</span>arrayItem<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">//获取数组中对象个数</span>
            <span class="token comment" spellcheck="true">//printf("cJSON_GetArraySize: size=%d\n",size); </span>

            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>object <span class="token operator">=</span> <span class="token function">cJSON_GetArrayItem</span><span class="token punctuation">(</span>arrayItem<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//获取父对象内容</span>
            <span class="token punctuation">{</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象1 */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"location"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\n-------------------------------location-----------------------------\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//匹配子对象1成员"id"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>   
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象1成员"name"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象1成员"country"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"country"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象1成员"timezone"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"timezone"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象1成员"timezone_offset"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"timezone_offset"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象2 */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"now"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"---------------------------------now-------------------------------\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//匹配子对象2成员"text"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"text"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"code"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"code"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"temperature"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"temperature"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"feels_like"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"feels_like"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"pressure"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"pressure"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"humidity"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"humidity"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"visibility"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"visibility"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"wind_direction"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"wind_direction"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"wind_speed"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"wind_speed"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"wind_scale"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"wind_scale"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"clouds"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"clouds"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token comment" spellcheck="true">//匹配子对象2成员"dew_point"</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>item <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>subobject<span class="token punctuation">,</span><span class="token string">"dew_point"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> 
                    <span class="token punctuation">{</span>
                        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n"</span><span class="token punctuation">,</span>item<span class="token operator">-></span>string<span class="token punctuation">,</span>item<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token comment" spellcheck="true">/* 匹配子对象3 */</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>subobject <span class="token operator">=</span> <span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span><span class="token string">"last_update"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
                <span class="token punctuation">{</span>
                    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"----------------------------last_update----------------------------\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s : %s\n\n"</span><span class="token punctuation">,</span>subobject<span class="token operator">-></span>string<span class="token punctuation">,</span>subobject<span class="token operator">-></span>valuestring<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span> 
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token function">cJSON_Delete</span><span class="token punctuation">(</span>json<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//释放cJSON_Parse()分配出来的内存空间</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>这个测试程序会去读取我们工程目录下的    <code>now.txt</code>件，所以事先我们需要把JSON格式的天气预报数据复制到该文件中：</p>
<p><img src="https://s2.ax1x.com/2019/06/24/ZkNa79.png" alt></p>
<p>把    <code>now.txt</code>里面的数据读出并保存到<code>data</code>指向的动态内存中。然后再把<code>data</code>中的数据传入我们事先编写好的解析天气数据的函数<code>int cJSON_WeatherParse(char *JSON)</code>中进行解析，最后把解析之后的数据给到该函数的返回值即可。</p>
<p>解析函数里主要用到以下函数：<br><strong>1、cJSON_Parse函数</strong></p>
<pre class=" language-c"><code class="language-c">cJSON<span class="token operator">*</span><span class="token function">cJSON_Parse</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>该函数用来解析JSON数据包，并按照cJSON结构体的结构序列化整个数据包。</p>
<p><strong>2、cJSON_GetObjectItem函数</strong></p>
<pre class=" language-c"><code class="language-c"><span class="token function">cJSON_GetObjectItem</span><span class="token punctuation">(</span>cJSON <span class="token operator">*</span>object<span class="token punctuation">,</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>string<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>该函数可从cJSON结构体中查找某个子节点名称（键名称），如果查找成功可把该子节点序列化到cJSON结构体中。</p>
<p><strong>3、cJSON_GetArraySize函数</strong></p>
<pre class=" language-c"><code class="language-c"><span class="token function">cJSON_GetArraySize</span><span class="token punctuation">(</span><span class="token keyword">const</span> cJSON <span class="token operator">*</span>array<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>该函数可获取数组中元素个数。</p>
<p><strong>4、cJSON_GetArrayItem函数</strong></p>
<pre class=" language-c"><code class="language-c"><span class="token function">cJSON_GetArrayItem</span><span class="token punctuation">(</span><span class="token keyword">const</span> cJSON <span class="token operator">*</span>array<span class="token punctuation">,</span> <span class="token keyword">int</span> index<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>该函数可获取数组中的内容。</p>
<p><strong>5、cJSON_Delete函数</strong></p>
<pre class=" language-c"><code class="language-c"><span class="token function">cJSON_Delete</span><span class="token punctuation">(</span>cJSON <span class="token operator">*</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre>
<p>该函数用来释放<code>cJSON_Parse</code>函数内部申请的堆内存。</p>
<p>我们的解析函数主要运用多次<code>cJSON_GetObjectItem</code>来匹配各对象成员，然后取出各个键值对的值<code>valuestring</code>。</p>
<p>该程序的运行结果如下：</p>
<p><a href="https://imgchr.com/i/ZkUiB4" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkUiB4.md.png" alt></a></p>
<p>可见，解析完全正确！解析结果中冒号后面的数据就是我们可以选择使用的数据。这是解析当天的天气实况数据，解析未来几天的天气数据包或是其它天气数据包的方法都是类似的。</p>
<h2 id="2-2-显示部分"><a href="#2-2-显示部分" class="headerlink" title="2.2 显示部分"></a>2.2 显示部分</h2><h3 id="2-2-1-几类常用的显示屏"><a href="#2-2-1-几类常用的显示屏" class="headerlink" title="2.2.1 几类常用的显示屏"></a>2.2.1 几类常用的显示屏</h3><p>液晶显示屏的接口较为常见的有 3 种类型：<code>RGB 接口</code>，<code>MCU 总线接口</code>，<code>串口 HMI</code>。</p>
<p><strong>（1）RGB 接口</strong></p>
<p><a href="https://imgchr.com/i/ZkaSIA" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkaSIA.md.png" alt></a></p>
<p>RGB 接口必须用在<code>带有RGB驱动的ARM芯片</code>上，一般的 ARM9 芯片有少许支持 RGB 的，ARM9 以上的芯片多数支持 RGB.但是此类接口的<code>驱动是最复杂</code>的，对<code>硬件要求也是最高</code>的。</p>
<p><strong>（2）MCU 总线接口</strong></p>
<p><a href="https://imgchr.com/i/Zkauin" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/Zkauin.md.png" alt></a></p>
<p>MCU 总线接口驱动比 RGB 简单一些，对硬件也基本没有任何要求，简单的 MCU 就可以驱动。但是界面的显示<code>驱动工作量很大</code>。</p>
<p>总线型接口的屏只提供点阵的操作。图片，字符等任何显示内容都是通过取模数据，在屏幕上相应的位置把点阵一个一个的打出来。在此基础上再来实现人机界面的逻辑。工作量很大。</p>
<p><strong>（3）串口 HMI</strong></p>
<p><a href="https://imgchr.com/i/Zkawz6" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/Zkawz6.md.png" alt></a></p>
<p>串口HMI是一种新的显示方案。首先它跟MCU总线屏一样<code>对硬件没有任何要求</code>，其次。它<code>没有速度瓶颈</code>，因为界面的显示是设备内部自己实现的，用户MCU只是发送指令，并不需要底层驱动。</p>
<p>再次，针对显示的人机界面的布局和大多数的逻辑（比如界面背景，按钮效果，文本显示等）。全部都不需要用户的 MCU 参与，使用设备提供的上位软件，在电脑上点几下鼠标就完成了。制作好资源文件以后下载到屏幕即可自动运行，剩下的就是 USART 交互了。</p>
<h3 id="2-2-2-本系统的显示方案"><a href="#2-2-2-本系统的显示方案" class="headerlink" title="2.2.2 本系统的显示方案"></a>2.2.2 本系统的显示方案</h3><p>本系统选择的是<code>串口HMI</code>这一显示方案。因为这种方案确实是可以在短时间内设计出比较漂亮的GUI界面。GUI界面设计软件如下图：</p>
<p><a href="https://imgchr.com/i/Zkacod" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/Zkacod.md.png" alt></a></p>
<p>这是串口屏商家给的配套的GUI设计软件，该软件的下载链接：</p>
<blockquote>
<p>链接：<a href="https://pan.baidu.com/s/1uYFhF412WVkk0FuqeOAGCg" target="_blank" rel="noopener">https://pan.baidu.com/s/1uYFhF412WVkk0FuqeOAGCg</a><br>提取码：e70r </p>
</blockquote>
<p>我们可以从左侧的工具箱里往工作区里拖拽需要用到的控件，常用的控件有文本控件、文字控件、按钮控件等。可在右侧的属性窗口设置控件的属性。可以通过选择不同的字库来设置不同的字体样式。</p>
<p>控件、页面的切换或则触发可能会产生相应的事件，可以通过代码来控制。其中，页面、控件的背景是可以上传本地的图片的，所以可以事先通过PS或则其他作图软件设计出精美的背景图片，然后再把控件都设置为透明色，最终地显示效果就可以达到很好的效果。</p>
<p>总之，可以很方便很容易设计出精美的GUI界面。同时，这个GUI设计软件还具有模拟真实的屏幕的功能，可以很方便地与用户MCU进行联调。仿真界面如图所示：</p>
<p><a href="https://imgchr.com/i/Zka5y8" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/Zka5y8.md.png" alt></a></p>
<p>进入模拟器界面，可在下方选择数据的输入方式为<code>用户MCU输入</code>，然后设置相应的串口号和波特率即可。还可以实时查看用户MCU传给模拟器的数据。</p>
<h2 id="2-3-与天气服务器通信"><a href="#2-3-与天气服务器通信" class="headerlink" title="2.3 与天气服务器通信"></a>2.3 与天气服务器通信</h2><p>每个问题的解决往往都不能一步到位，要把这个问题的所有关键点找出来，着手去解决这些关键点，最终问题自然会得到解决。</p>
<p>同样的，虽然我们最终是用单片机控制WiFi模块来获取天气数据的，但是我们首先应该确保在没有单片机的情况下能获取得到天气数据，确保能和天气服务器正常通信。只有这样，在使用单片机获取数据遇到问题时才知道出错的范围在哪，便于我们进行调试。下面，分享windows下与天气服务器通信的测试方法：</p>
<h3 id="2-3-1-所需的工具"><a href="#2-3-1-所需的工具" class="headerlink" title="2.3.1 所需的工具"></a>2.3.1 所需的工具</h3><p><code>网络调试助手</code>。本人使用的是<code>SocketTool</code>，<code>SocketTool</code>是一款小巧实用且功能强大的<code>TCP/UDP</code>网络通讯调试工具，可以帮助你检查网络应用软件及硬件的通讯情况，可以创建Socket服务器，如创建UDP组播地址及端口、创建UDP Client客户端、创建TCP Client、创建TCP Server。</p>
<p><a href="https://imgchr.com/i/ZkdbnO" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/ZkdbnO.md.png" alt></a></p>
<p>该工具下载链接为：</p>
<blockquote>
<p>链接：<a href="https://pan.baidu.com/s/1fgarl8xNb6nEAl3Ly0NQ3w" target="_blank" rel="noopener">https://pan.baidu.com/s/1fgarl8xNb6nEAl3Ly0NQ3w</a><br>提取码：np5v </p>
</blockquote>
<h3 id="2-3-2-测试方法"><a href="#2-3-2-测试方法" class="headerlink" title="2.3.2 测试方法"></a>2.3.2 测试方法</h3><p>（1）首先，使用<code>SocketTool</code>工具建立一个<code>TCP Client</code>，对方IP设为：<code>116.62.81.138</code>（这是心知天气服务器的IP地址），对方端口设为80。如：</p>
<p><img src="http://i67.tinypic.com/34eytmr.gif" alt></p>
<p>怎么才能知道一个网站的IP呢？在DOS黑窗口下输入<code>ping+域名</code>即可得该域名对应的IP，如我们<code>ping</code>百度：</p>
<p><img src="https://s2.ax1x.com/2019/06/24/Zk08L8.png" alt></p>
<p><code>ping</code>是Windows、Unix和Linux系统下的一个命令，利用ping命令可以检查网络是否连通，可以很好地帮助我们分析和判定网络故障，该命令还可以加许多参数使用，具体是键入Ping按回车即可看到详细说明。</p>
<p>（2）发送HTTP请求，向心知天气服务器请求天气数据。HTTP有几种请求方法，我们这里使用的是GET请求：</p>
<blockquote>
<p>GET请求：从指定的资源请求数据。</p>
</blockquote>
<p>具体的请求方法示例为：</p>
<pre class=" language-c"><code class="language-c">GET https<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>api<span class="token punctuation">.</span>seniverse<span class="token punctuation">.</span>com<span class="token operator">/</span>v3<span class="token operator">/</span>weather<span class="token operator">/</span>now<span class="token punctuation">.</span>json<span class="token operator">?</span>key<span class="token operator">=</span>2owqvhhd2dd9o9f9<span class="token operator">&amp;</span>location<span class="token operator">=</span>beijing<span class="token operator">&amp;</span>language<span class="token operator">=</span>zh<span class="token operator">-</span>Hans<span class="token operator">&amp;</span>unit<span class="token operator">=</span>c</code></pre>
<p>其中，GET后面的URL地址可以上心知天气查看，如：</p>
<p><a href="https://imgchr.com/i/Zk0EqO" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/Zk0EqO.md.png" alt></a></p>
<p>URL中的几个参数是可以设置的：</p>
<blockquote>
<p>key：你的API密钥<br>location：所查询的地点<br>language：语言<br>unit：单位</p>
</blockquote>
<p>在<code>SocketTool</code>工具中发送<code>GET请求</code>（首先先得点击<code>连接</code>按钮进行连接），发送格式如下：</p>
<p><a href="https://imgchr.com/i/Zk0toQ" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/Zk0toQ.md.png" alt></a></p>
<p>需要注意的问题就是每个GET请求之后都需要空两行，这两个换行也是GET请求的一部分，所以在编写单片机代码时需要注意的是要在这个请求字符串后添加<code>\r\n\r\n</code>，表示换两行。</p>
<p>服务器返回的天气数据为：</p>
<p><a href="https://imgchr.com/i/Zk0dWn" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/24/Zk0dWn.md.png" alt></a></p>
<p>看看服务器返回的数据，发现中文都是乱码。原因是获取得的天气数据是utf-8格式，必须转换为GBK格式中文才能正常显示。此处，我们只是测试与服务器是否能正常交互，测试结果显示有数据返回，说明通过以上的GET请求时可以获取到数据的。</p>
<p>在应用到单片机上时，还需要考虑的问题就是怎么把utf-8格式转换为GBK格式的问题，转换后中文才能正常显示在显示屏上。</p>
<h2 id="2-4-语音对话功能"><a href="#2-4-语音对话功能" class="headerlink" title="2.4 语音对话功能"></a>2.4 语音对话功能</h2><p>本系统人机对话功能采用了两个硬件模块：（1）语音识别模块：采用<code>LD3320</code>语音识别芯片；（2）语音合成模块：采用<code>SYN6288</code>语音合成芯片。</p>
<h3 id="2-4-1-语音识别"><a href="#2-4-1-语音识别" class="headerlink" title="2.4.1 语音识别"></a>2.4.1 语音识别</h3><p>本系统语音识别模块采用的语音芯片是LD3320。该芯片已经集成了语音识别的处理器，不需要外接其他的辅助芯片如Flash、 RAM 等，直接嵌入在现有的产品中就可以实现语音识别的功能。</p>
<p>语音识别的过程为：<br>（1）先预存要识别的关键词，如：</p>
<pre class=" language-c"><code class="language-c"><span class="token comment" spellcheck="true">//-------------------------搜索天气-----------------------------------</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR00  "xiao tian"                    </span><span class="token comment" spellcheck="true">// 小天</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR01  "sou suo fu zhou tian qi"      </span><span class="token comment" spellcheck="true">// 搜索福州天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR02  "sou suo shang hai tian qi"    </span><span class="token comment" spellcheck="true">// 搜索上海天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR03  "sou suo shen zhen tian qi"    </span><span class="token comment" spellcheck="true">// 搜索深圳天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR04  "sou suo bei jing tian qi"     </span><span class="token comment" spellcheck="true">// 搜索北京天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR05  "sou suo guang zhou tian qi"   </span><span class="token comment" spellcheck="true">// 搜索广州天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR06  "sou suo nan ning tian qi"     </span><span class="token comment" spellcheck="true">// 搜索南宁天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR07  "sou suo xia men tian qi"      </span><span class="token comment" spellcheck="true">// 搜索厦门天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR08  "sou suo quan zhou tian qi"    </span><span class="token comment" spellcheck="true">// 搜索泉州天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR09  "sou suo pu tian tian qi"      </span><span class="token comment" spellcheck="true">// 搜索莆田天气</span>
<span class="token macro property">#<span class="token directive keyword">define</span>  STR10  "sou suo nan ping tian qi"     </span><span class="token comment" spellcheck="true">// 搜索南平天气</span></code></pre>
<p>可以预存50条关键词（关键句），本人已经把关键词写死在程序里了，这显然就不能灵活的面对各种场景。其实可以通过代码编写一个学习功能，即识别之前首先进行学习一些即将要识别的关键词，然后在进行识别演示，这样就可以应对比较多的场景。</p>
<p>但是，这样还是不够智能，毕竟只能识别已经预存的关键词（关键句），要是没有预存就没办法识别了。所以真正的语音识别应该是在软件算法上下功夫，关于语音识别已然成为热门的一大研究专题，这就属于人工智能的范畴吧。希望以后可以有机会接触这一块，如有接触再做学习分享~</p>
<p>（2）开始识别，如：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">Task_ASR</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
 <span class="token keyword">switch</span><span class="token punctuation">(</span>nAsrStatus<span class="token punctuation">)</span>
 <span class="token punctuation">{</span>
   <span class="token keyword">case</span> LD_ASR_RUNING<span class="token punctuation">:</span>    
   <span class="token keyword">case</span> LD_ASR_ERROR<span class="token punctuation">:</span>    
     <span class="token keyword">break</span><span class="token punctuation">;</span>
   <span class="token keyword">case</span> LD_ASR_NONE<span class="token punctuation">:</span>
     nAsrStatus<span class="token operator">=</span>LD_ASR_RUNING<span class="token punctuation">;</span> 
     <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">RunASR</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true">//  启动一次ASR识别流程：ASR初始化，ASR添加关键词语，启动ASR运算</span>
     <span class="token punctuation">{</span>    
       nAsrStatus <span class="token operator">=</span> LD_ASR_ERROR<span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
     <span class="token keyword">break</span><span class="token punctuation">;</span>

   <span class="token keyword">case</span> LD_ASR_FOUNDOK<span class="token punctuation">:</span>
     nAsrRes <span class="token operator">=</span> <span class="token function">LD_GetResult</span><span class="token punctuation">(</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment" spellcheck="true">//  一次ASR识别流程结束，去取ASR识别结果  </span>
     <span class="token function">ASRSuccess_Handle</span><span class="token punctuation">(</span>nAsrRes<span class="token punctuation">)</span><span class="token punctuation">;</span>
     nAsrStatus <span class="token operator">=</span> LD_ASR_NONE<span class="token punctuation">;</span>  
   <span class="token keyword">break</span><span class="token punctuation">;</span>

   <span class="token keyword">case</span> LD_ASR_FOUNDZERO<span class="token punctuation">:</span>
   <span class="token keyword">default</span><span class="token punctuation">:</span>
     nAsrStatus <span class="token operator">=</span> LD_ASR_NONE<span class="token punctuation">;</span>  
     <span class="token keyword">break</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p><code>nAsrStatus</code>是用来表示语音识别的状态，不是<code>LD3320</code>芯片内部的状态寄存器。<code>nAsrStatus</code>有几种情况。我们比较关注的是<code>LD_ASR_FOUNDOK</code>状态。<code>LD_ASR_FOUNDOK</code>状态为识别成功，识别成功后将调用<code>ASRSuccess_Handle</code>函数进行识别后的操作。</p>
<p>（3）识别成功则执行相应操作，如</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">ASRSuccess_Handle</span><span class="token punctuation">(</span>uint8 asr_code<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
 <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\r\n识别码:%d\n"</span><span class="token punctuation">,</span>asr_code<span class="token punctuation">)</span><span class="token punctuation">;</span>    
 <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token number">0</span> <span class="token operator">==</span> asr_code<span class="token punctuation">)</span>
 <span class="token punctuation">{</span>
   <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"我在，需要我的帮助吗？\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]我在,[2]需要[2]我的[3]帮助吗"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   RunFlag <span class="token operator">=</span> TRUE<span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
 <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>RunFlag<span class="token punctuation">)</span>
 <span class="token punctuation">{</span>
   RunFlag <span class="token operator">=</span> FALSE<span class="token punctuation">;</span>
   <span class="token comment" spellcheck="true">/* 识别码0-10为搜索天气识别码 */</span>
   <span class="token keyword">if</span><span class="token punctuation">(</span>asr_code<span class="token operator">>=</span><span class="token number">0</span><span class="token operator">&amp;&amp;</span>asr_code<span class="token operator">&lt;=</span><span class="token number">10</span><span class="token punctuation">)</span>
   <span class="token punctuation">{</span>
     <span class="token keyword">switch</span><span class="token punctuation">(</span>asr_code<span class="token punctuation">)</span>      
     <span class="token punctuation">{</span>
       <span class="token keyword">case</span> CODE01<span class="token punctuation">:</span>      
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“福州”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索福州天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"fujianfuzhou"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE02<span class="token punctuation">:</span>   
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“上海”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索上海天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"shanghai"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE03<span class="token punctuation">:</span>    
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“深圳”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索深圳天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"shenzhen"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE04<span class="token punctuation">:</span>    
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“北京”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索北京天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"beijing"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE05<span class="token punctuation">:</span>    
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“广州”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索广州天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"guangzhou"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE06<span class="token punctuation">:</span>    
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“南宁”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索南宁天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"nanning"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE07<span class="token punctuation">:</span>    
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“厦门”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索厦门天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"xiamen"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE08<span class="token punctuation">:</span>    
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“泉州”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索泉州天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"quanzhou"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE09<span class="token punctuation">:</span>    
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“莆田”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索莆田天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"putian"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE10<span class="token punctuation">:</span>  
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“南平”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索南平天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
         <span class="token function">memcpy</span><span class="token punctuation">(</span>g_city<span class="token punctuation">,</span><span class="token string">"nanping"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>g_place<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
     <span class="token function">memset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>weather_data<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>weather_data<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token function">GET_NowWeather</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token function">GET_DailyWeather</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     GetWeatherTimer <span class="token operator">=</span> TIMER1_HOUR<span class="token punctuation">;</span>  
     <span class="token function">DisplayWeather</span><span class="token punctuation">(</span>weather_data<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token function">DisplayWeatherIcon</span><span class="token punctuation">(</span>weather_data<span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>
   <span class="token keyword">else</span>
   <span class="token punctuation">{</span>
     <span class="token keyword">switch</span><span class="token punctuation">(</span>asr_code<span class="token punctuation">)</span>  
     <span class="token punctuation">{</span>
       <span class="token keyword">case</span> CODE11<span class="token punctuation">:</span>
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“语音播报天气”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s\n"</span><span class="token punctuation">,</span>g_WeatherText<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//          TTSPlay(0, (uint8_t*)g_WeatherText);</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">case</span> CODE12<span class="token punctuation">:</span>
         <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"“今天的气温是多少”命令识别成功\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
       <span class="token keyword">default</span><span class="token punctuation">:</span>
         <span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"语音识别失败，请对准麦克风说话！"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">break</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
   <span class="token punctuation">}</span>

 <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<h3 id="2-4-2-语音合成"><a href="#2-4-2-语音合成" class="headerlink" title="2.4.2 语音合成"></a>2.4.2 语音合成</h3><p>本系统的语音合成模块采用SYN6288语音合成芯片，支持文本直接转化为语音。其与单片机的通信方式为串口通信。向该模块发送以下格式的数据包：</p>
<blockquote>
<p>5字节帧头+文本+1字节校验，文本字节数小于等于200字节</p>
</blockquote>
<p>即可合成语音。代码如：</p>
<pre class=" language-c"><code class="language-c"><span class="token keyword">void</span> <span class="token function">TTSPlay</span><span class="token punctuation">(</span>uint8_t Music<span class="token punctuation">,</span>uint8_t <span class="token operator">*</span>Text<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
 <span class="token comment" spellcheck="true">/****************需要发送的文本**********************************/</span> 
 uint8_t DataPacket<span class="token punctuation">[</span><span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">;</span>    <span class="token comment" spellcheck="true">//</span>
 uint8_t Text_Len<span class="token punctuation">;</span>  
 uint8_t ecc  <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">//定义校验字节</span>
 uint8_t i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>  
 Text_Len <span class="token operator">=</span><span class="token function">strlen</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>Text<span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">//需要发送文本的长度</span>

 <span class="token comment" spellcheck="true">/*****************帧固定配置信息**************************************/</span>           
 DataPacket<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0xFD</span> <span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">//构造帧头FD</span>
 DataPacket<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0x00</span> <span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">//构造数据区长度的高字节</span>
 DataPacket<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> Text_Len <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">//构造数据区长度的低字节</span>
 DataPacket<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0x01</span> <span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">//构造命令字：合成播放命令          </span>
 DataPacket<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0x01</span> <span class="token operator">|</span> Music<span class="token operator">&lt;&lt;</span><span class="token number">4</span> <span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">//构造命令参数：背景音乐设定</span>

 <span class="token comment" spellcheck="true">/*******************校验码计算***************************************/</span>     
 <span class="token keyword">for</span><span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>           <span class="token comment" spellcheck="true">//依次发送构造好的5个帧头字节</span>
 <span class="token punctuation">{</span>  
   ecc<span class="token operator">=</span>ecc<span class="token operator">^</span><span class="token punctuation">(</span>DataPacket<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment" spellcheck="true">//对发送的字节进行异或校验  </span>
 <span class="token punctuation">}</span>

 <span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>Text_Len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>       <span class="token comment" spellcheck="true">//依次发送待合成的文本数据</span>
 <span class="token punctuation">{</span>  
   ecc<span class="token operator">=</span>ecc<span class="token operator">^</span><span class="token punctuation">(</span>Text<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">//对发送的字节进行异或校验    </span>
 <span class="token punctuation">}</span>     
 <span class="token comment" spellcheck="true">/*******************发送帧信息***************************************/</span>      
 <span class="token function">memcpy</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>DataPacket<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span> Text<span class="token punctuation">,</span> Text_Len<span class="token punctuation">)</span><span class="token punctuation">;</span>
 DataPacket<span class="token punctuation">[</span><span class="token number">5</span><span class="token operator">+</span>Text_Len<span class="token punctuation">]</span><span class="token operator">=</span>ecc<span class="token punctuation">;</span>
 <span class="token function">UART4_SendStr</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token punctuation">)</span>DataPacket<span class="token punctuation">,</span><span class="token number">5</span><span class="token operator">+</span>Text_Len<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>调用方式如：</p>
<pre class=" language-c"><code class="language-c"><span class="token function">TTSPlay</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"[t3][2]小天正在为您搜索福州天气"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<h2 id="3、代码获取"><a href="#3、代码获取" class="headerlink" title="3、代码获取"></a>3、代码获取</h2><p>以上就是关于该作品比较重要的一些内容的分享，由于篇幅太长其他功能不做介绍，有兴趣的朋友可以查看源码。源码链接：</p>
<blockquote>
<p><a href="https://github.com/zhengnianli/stm32_weather" target="_blank" rel="noopener">https://github.com/zhengnianli/stm32_weather</a></p>
</blockquote>
<hr>
<p>我的个人博客：<a href="https://www.lizhengnian.cn/">https://www.lizhengnian.cn/</a></p>
<p>我的微信公众号：嵌入式大杂烩</p>
<p>我的CSDN博客：<a href="https://blog.csdn.net/zhengnianli" target="_blank" rel="noopener">https://blog.csdn.net/zhengnianli</a></p>
<p><img src="https://s1.ax1x.com/2020/06/22/NGV7Oe.png" alt></p>
<p><a href="https://imgchr.com/i/VcSFJJ" target="_blank" rel="noopener"><img src="https://s2.ax1x.com/2019/06/11/VcSFJJ.md.png" alt></a></p>
<p>我的博客即将同步至腾讯云+社区，邀请大家一同入驻：<a href="https://cloud.tencent.com/developer/support-plan?invite_code=oeilcfscuui4" target="_blank" rel="noopener">https://cloud.tencent.com/developer/support-plan?invite_code=oeilcfscuui4</a></p>

            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">欢迎关注ZhengN的微信公众号</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <!--<ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微信公众号</a></li>
                    </ul>-->
					<!--
                    <div id="alipay">
                        <img src="" class="reward-img" alt="支付宝打赏二维码">
                    </div>
					-->
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信公众号二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            


        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments input[type=text],
    #vcomments input[type=email],
    #vcomments input[type=url],
    #vcomments textarea {
        box-sizing: border-box;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #42b983;
        font-weight: 500;
        text-decoration: underline;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div id="vcomments" class="card-content"></div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'Vc276uuqkd8y8heuqzShe9p1-gzGzoHsz',
        appKey: 'bUwy6lANpgeVdpcQamMtC8QD',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: '快来快活呀！^_^'
    });
</script>
    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fa fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2019/06/30/ji-suan-ji-wang-luo-tcp-udp-tong-xin-zong-jie/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/6.jpg" class="responsive-img" alt="【计算机网络】TCP、UDP通信总结">
                        
                        <span class="card-title">【计算机网络】TCP、UDP通信总结</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            一、什么是socket？Socket的英文原义是“孔”或“插座”。在编程中，Socket被称做套接字，是网络通信中的一种约定。Socket编程的应用无处不在，我们平时用的QQ、微信、浏览器等程序，都与Socket编程有关。我们平时使用浏览器
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2019-06-30
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/计算机网络/" class="post-category" target="_blank">
                                    计算机网络
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/socket/" target="_blank">
                        <span class="chip bg-color">socket</span>
                    </a>
                    
                    <a href="/tags/网络编程/" target="_blank">
                        <span class="chip bg-color">网络编程</span>
                    </a>
                    
                    <a href="/tags/TCP/" target="_blank">
                        <span class="chip bg-color">TCP</span>
                    </a>
                    
                    <a href="/tags/UDP/" target="_blank">
                        <span class="chip bg-color">UDP</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fa fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2019/06/15/zhi-shi-dian-json-de-jian-dan-ren-shi/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/11.jpg" class="responsive-img" alt="JSON的简单认识">
                        
                        <span class="card-title">JSON的简单认识</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            1、什么是JSON？JSON（JavaScript Object Notation）：JavaScript 对象表示法。其是一种轻量级的数据交换格式，简洁和清晰的层次结构使得其成为理想的数据交换语言。 易于人阅读和编写，同时也易于机器解析和
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2019-06-15
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/嵌入式/" class="post-category" target="_blank">
                                    嵌入式
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/嵌入式/" target="_blank">
                        <span class="chip bg-color">嵌入式</span>
                    </a>
                    
                    <a href="/tags/JSON/" target="_blank">
                        <span class="chip bg-color">JSON</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fa fa-list"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).slideUp(500);
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).slideDown(500);
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站由&copy;<a href="https://zhengnianli.github.io/" target="_blank">ZhengNian</a>基于
            <a href="https://hexo.io/" target="_blank">Hexo</a> 的
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">hexo-theme-matery</a>主题搭建.

            
                &nbsp;<i class="fa fa-area-chart"></i>&nbsp;站点总字数:&nbsp;
                <span class="white-color">187.2k</span>
            

            
			
                <br>
                
                <span id="busuanzi_container_site_pv">
                    <i class="fa fa-heart-o"></i>
                    本站总访问量 <span id="busuanzi_value_site_pv" class="white-color"></span>
                </span>
                
                
                <span id="busuanzi_container_site_uv">
                    <i class="fa fa-users"></i>
                    次,&nbsp;访客数 <span id="busuanzi_value_site_uv" class="white-color"></span> 人.
                </span>
                
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/zhengnianli" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fa fa-github"></i>
    </a>



    <a href="mailto:2238184039@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fa fa-envelope-open"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2238184039" class="tooltipped" data-tooltip="QQ联系我: 2238184039" data-position="top" data-delay="50">
        <i class="fa fa-qq"></i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fa fa-rss"></i>
    </a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fa fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->



    <script src="/libs/others/clicklove.js"></script>


    <script async src="/libs/others/busuanzi.pure.mini.js"></script>


</body>
</html>